package com.k8sops.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.k8sops.backend.common.ApiResponse;
import com.k8sops.backend.model.entity.DOSProjectGroup;
import com.k8sops.backend.service.DOSProjectGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Project Group Controller
 */
@RestController
@Api(tags = "项目组")
@RequestMapping("/project-group")
public class DOSProjectGroupController {

    @Autowired
    private DOSProjectGroupService projectGroupService;

    /**
     * Add a new project group
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加项目组", notes = "生成考情分析报告")
    public ApiResponse<Boolean> addProjectGroup(@RequestBody DOSProjectGroup projectGroup) {
        boolean result = projectGroupService.addProjectGroup(projectGroup);
        return result ? ApiResponse.success(true) : ApiResponse.error("添加项目组失败");
    }

    /**
     * Update project group information
     */
    @PutMapping("/update")
    public ApiResponse<Boolean> updateProjectGroup(@RequestBody DOSProjectGroup projectGroup) {
        if (projectGroup.getId() == null) {
            return ApiResponse.error("项目组ID不能为空");
        }
        boolean result = projectGroupService.updateProjectGroup(projectGroup);
        return result ? ApiResponse.success(true) : ApiResponse.error("更新项目组失败");
    }

    /**
     * Delete a project group by ID
     */
    @DeleteMapping("/delete/{id}")
    public ApiResponse<Boolean> deleteProjectGroup(@PathVariable Long id) {
        boolean result = projectGroupService.deleteProjectGroupById(id);
        return result ? ApiResponse.success(true) : ApiResponse.error("删除项目组失败");
    }

    /**
     * Get project group by ID
     */
    @GetMapping("/get/{id}")
    public ApiResponse<DOSProjectGroup> getProjectGroup(@PathVariable Long id) {
        DOSProjectGroup projectGroup = projectGroupService.getProjectGroupById(id);
        return projectGroup != null ? ApiResponse.success(projectGroup) : ApiResponse.error("项目组不存在");
    }

    /**
     * Page query project groups
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取项目组里列表", notes = "获取项目组里列表")
    public ApiResponse<IPage<DOSProjectGroup>> pageProjectGroups(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String groupName) {
        IPage<DOSProjectGroup> result = projectGroupService.pageProjectGroups(page, size, groupName);
        return ApiResponse.success(result);
    }
}
